package com.dch.calculator.activities;

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.dch.calculator.R;
import com.dch.calculator.activities.log.LogActivity;
import com.dch.calculator.listeners.MenuListItemListener;

import java.util.ArrayList;

public class MainActivity extends LogActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.main_menu);

            ListView listView = (ListView) findViewById(R.id.menu_list);
            if (listView != null) {
                ArrayList<String> names = fillMenuNames();
                int length = names.size();
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.main_menu_item,
                        names.toArray(new String[length]));
                Log.d(LOG_TAG, "set adapter");
                listView.setAdapter(adapter);
                Log.d(LOG_TAG, "set item click listener");
                listView.setOnItemClickListener(new MenuListItemListener(this, names));
            } else {
                Log.d(LOG_TAG, "list view is null");
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showCalculator() {
        super.showCalculator();
        Toast.makeText(this, "open calculator", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showAboutProgram() {
        super.showAboutProgram();
        Toast.makeText(this, "open about info", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showSettings() {
        super.showSettings();
        Toast.makeText(this, "open settings", Toast.LENGTH_SHORT).show();
    }

    private ArrayList<String> fillMenuNames() {
        ArrayList<String> names = new ArrayList<String>();
        names.add(getResources().getString(R.string.calculator));
        names.add(getResources().getString(R.string.settings));
        names.add(getResources().getString(R.string.about));
        names.add(getResources().getString(R.string.exit));
        return names;
    }

}
